var countStr; //点亮数
var titleName; //业务员姓名

/*
var share_title = '奔跑吧，春节！组队撕名牌，iPhone6 Plus就是你的！';
var share_desc = '100%中奖的名牌赶紧来撕！';
var share_link = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx497082f7a8e4b806&redirect_uri='+
		'http%3A%2F%2Fwww.ladyflora.com%2FweiChartCard%2FweiActive%2Fshare_active.html%3Forigopenid%3D'+origopenid
		+'%26lastopenid%3D'+lastopenid
		+'&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect';
		*/
var code = getQueryString("code");
var luckdrawid;
//初始化业务员数据
$(function(){
	//判断活动是否结束
	//active_status();
	$.ajax({
		url: API_URL+'agenttop/findagent?openid='+origopenid,
		type: 'get',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
				var myData = obj.data;
				if(myData[0].icon == null || myData[0].icon == ''){
					myData[0].icon = '../img/default.png';
				}
				
				titleName = myData[0].name;
				//顶部微名片
				var agentStr = '<p><span class="name">'+myData[0].name+'</span>'+myData[0].phone;
				agentStr += '<img src="'+myData[0].icon+'" alt=""/></p>'
				$('.head_banner').html(agentStr);
				
				//隐藏div中的微名片
				var wmpStr = '<p class="head_logo_content"><span class="xinhua_logo"></span><em>微名片</em></p>';
				wmpStr += '<img src="'+myData[0].icon+'" alt="">';
				wmpStr += '<h3 class="per_name">'+myData[0].name+'</h3>';
				wmpStr += '<p class="per_tel">'+myData[0].phone+'</p>';
				wmpStr += '<input type="button" id="tel_call">';
				$('.person_inforcontent').html(wmpStr);
				//根据不同的身份显示不同的内容
				active_load();
				

			}else if (obj.status.code == 9999){
				//如果已结束,则直接跳转页面
				window.location = 'active_index.html?openid='+origopenid;
			}else{
				alert('服务器错误');
			}
		},
		error: function (data){
			alert('333');
			console.log("服务器异常!");
		}
	});

	
	var share_obj = {
		title : share_title,
		desc : share_desc,
		link : share_link
	}
	//wx_share_element.init(share_obj);
	//加载JS动态数据
	show_load();
	//加载随机生成手机号
	randomPhone();
});


//判断活动是否已结束,并返回有多少人参与
function active_status(){
	$.ajax({
		url: API_URL+'activities/isactivities',
		type: 'get',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if(obj.status.code == 9999) {
				//如果已结束,则直接跳转页面
				window.location = 'active_index.html?openid='+origopenid;
			}
		},
		error: function (data){
			alert('服务器错误,请稍后再试!');
		}
	});
	
}


//根据不同的身份显示不同的内容
function active_load(){
	//有code, 说明是点击分享连接进来的
	if(code!=null && code!=''){
		$.ajax({
			url: API_URL+'share/pleaseshare?origopenid='+origopenid+'&lastopenid='+lastopenid+'&code='+code,
			type: 'get',
			dataType: "JSON",
			data:'',
			async: false,
			success: function (obj){
				openid = obj.basedata.openid;
				//alert('分享人:'+lastopenid+'---->点击人:'+openid);
				if (obj.status.code == 0 || obj.status.code == 1 || obj.status.code == 2 || obj.status.code == -3) {
				//成功帮忙点亮
				//调用点亮列表接口
				if(origopenid!=openid){
					dianliang_list();
				}

				}else if(obj.status.code == 3){
					//我分享过了. 但点击的还是业务员给的连接, 按分享人是自己的流程
					if(origopenid!=openid){
						lastopenid = openid;
						dianliang_list();
					}

				}else if(obj.status.code == 4){
					//我没分享过. 但点击的还是业务员给的连接
					if(origopenid==openid){
						//业务员自己
						$('.bord_cover').html('<p>您拥有了名牌奖券，</p>快将福利转发给客户吧~');
						$('#button_share').html('<input type="button" id="share_btn" class="trriger"/>');
						
						$('.bord_cover').on('click',function(){
							$('body').scrollTop(0);
							$('.suggest_mask').fadeIn('fast');
						});
					}else{
						//其他人
						$('.bord_cover').html('<h3>'+titleName+'赠送您一张名牌奖券</h3><p class="infor">邀请好友点亮头像，撕名牌100%中奖！</p>');
						$('#button_share').html('<input type="button" id="call_friend_btn"/>');
						
						$('.bord_cover').on('click',function(){
							$('body').scrollTop(0);
							$('.suggest_mask').fadeIn('fast');
						});
					}
			}else if (obj.status.code == 9999){
				//如果已结束,则直接跳转页面
				window.location = 'active_index.html?openid='+origopenid;
			}else{
					alert('服务器错误');
				}
			},
			error: function (data){
				console.log("服务器异常!");
			}
		});
		if(origopenid!=openid){
			share_title = '快来帮帮我~就差你点亮啦！我要中iPhone6 Plus~';
			share_desc = '奔跑吧，春节！100%中奖的名牌赶紧来撕！';
			share_link = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx497082f7a8e4b806&redirect_uri='+
			'http%3A%2F%2Fwww.ladyflora.com%2FweiChartCard%2FweiActive%2Fshare_active.html%3Forigopenid%3D'+origopenid
			+'%26lastopenid%3D'+openid
			+'&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect';
		}
	}else{
		openid = lastopenid;
		if(origopenid!=lastopenid){
			var titleStr = '<h3>'+titleName+'赠送您一张名牌奖券</h3><p class="infor">邀请好友点亮头像，撕名牌100%中奖！</p>';
			var buttonStr = '<input type="button" id="call_friend_btn"/>';
			$('.bord_cover').html(titleStr);
			$('#button_share').html(buttonStr);
			
			dianliang_list();
			
			share_title = '快来帮帮我~就差你点亮啦！我要中iPhone6 Plus~';
			share_desc = '奔跑吧，春节！100%中奖的名牌赶紧来撕！';
			share_link = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx497082f7a8e4b806&redirect_uri='+
			'http%3A%2F%2Fwww.ladyflora.com%2FweiChartCard%2FweiActive%2Fshare_active.html%3Forigopenid%3D'+origopenid
			+'%26lastopenid%3D'+openid
			+'&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect';
		}
	}
}

//点亮人列表
function dianliang_list(){
	$.ajax({
		url: API_URL+'share/findshare?lastopenid='+lastopenid,
		type: 'get',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 0) {
				var myData = obj.data;
				countStr = myData.length;
				var titleStr = '<h3>'+titleName+'赠送您一张名牌奖券</h3><p class="infor">邀请好友点亮头像，撕名牌100%中奖！</p>';
				var buttonStr = '<input type="button" id="call_friend_btn"/>';

				if(myData.length!=0){
					if(lastopenid == openid){
						buttonStr = '<input type="button" id="call_friend_btn"/>'; //立即邀请好友,我也要参加,立即分享
					}else{
						buttonStr = '<input type="button" id="play_into"/>'; //立即邀请好友,我也要参加,立即分享
					}
					
					var Str = '';
					Str = '<p class="title">为TA点亮头像的好友</p>';
					Str += '<ul>';

					for(var i=0; i < myData.length; i++){
						if(i==3){
							break;
						}
						if(myData[i].icon == null || myData[i].icon == ''){
							myData[i].icon = '../img/linght_default.png';
						}
						Str += '<li>';
						Str += '<img src="'+myData[i].icon+'" alt="">';
						Str += '<h3 class="name">'+myData[i].nickname+'</h3>';
						Str += '<p class="time">'+new Date(myData[i].createtime).format("yyyy-MM-dd hh:mm")+'</p>';
						if(i==0){
							Str += '<p class="status">为我点亮了“<em>超超</em>”</p>';
							if(myData[i].fastopenid == openid){
								titleStr = '<h3>您为TA成功点亮“超超”</h3><p class="infor">现在参与，点头像、撕名牌、赢大奖！</p>';
							}
						}
						if(i==1){
							Str += '<p class="status">为我点亮了“<em>东方BB</em>”</p>';
							if(myData[i].fastopenid == openid){
								titleStr = '<h3>您为TA成功点亮“东方BB”</h3><p class="infor">现在参与，点头像、撕名牌、赢大奖！</p>';
							}
						}
						if(i==2){
							Str += '<p class="status">为我点亮了“<em>小猎豹</em>”</p>';
							if(myData[i].fastopenid == openid){
								titleStr = '<h3>您为TA成功点亮“小猎豹”</h3><p class="infor">现在参与，点头像、撕名牌、赢大奖！</p>';
							}else{
								titleStr = '<h3>您的头像已全部点亮！</h3><p class="infor">赶快撕名牌抽大奖吧！</p>';
							}
						}
						Str += '</li>';
					}
					Str += '</ul>';
					$('.friend_linght.clear').html(Str);
				}

				$('.bord_cover').html(titleStr);
				$('#button_share').html(buttonStr);
				show_lock();
			}else if (obj.status.code == 9999){
				//如果已结束,则直接跳转页面
				window.location = 'active_index.html?openid='+origopenid;
			}else{
				alert('服务器错误');
			}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}

//点亮图标
function show_lock(){
	var dlStr; //点亮图标
	if(countStr==1){
		dlStr = '<li>'+
					'<img class="linght_load" src="../img/loding_circle.png" alt=""/>'+
					'<img src="../img/pao_1.png" alt="">'+
				 '</li>'+
				 '<li>'+
					'<img src="../img/pao_22.png" alt="">'+
				 '</li>'+
				 '<li>'+
			     '<img src="../img/pao_33.png" alt="">'+
				 '</li>';
	}

	if(countStr==2){
		dlStr = '<li>'+
					'<img class="linght_load" src="../img/loding_circle.png" alt=""/>'+
					'<img src="../img/pao_1.png" alt="">'+
				 '</li>'+
				 '<li>'+
					'<img class="linght_load" src="../img/loding_circle.png" alt=""/>'+
					'<img src="../img/pao_2.png" alt="">'+
				 '</li>'+
				 '<li>'+
			     '<img src="../img/pao_33.png" alt="">'+
				 '</li>';
	}

	if(countStr==3){
		dlStr = '<li>'+
					'<img class="linght_load" src="../img/loding_circle.png" alt=""/>'+
					'<img src="../img/pao_1.png" alt="">'+
				 '</li>'+
				 '<li>'+
					'<img class="linght_load" src="../img/loding_circle.png" alt=""/>'+
					'<img src="../img/pao_2.png" alt="">'+
				 '</li>'+
				 '<li>'+
				 '<img class="linght_load" src="../img/loding_circle.png" alt=""/>'+
			     '<img src="../img/pao_3.png" alt="">'+
				 '</li>';

	}
	
	$('.person_content').html(dlStr);
	
	
	if(countStr>=3){
		//查看是否中奖
		lock_status();
	}else{
		$('.bord_cover').on('click',function(){
			$('body').scrollTop(0);
			$('.suggest_mask').fadeIn('fast');
		});
	}
}


//查看是否中奖
function lock_status(){
	$.ajax({
		url: API_URL+'luckdraw/isluckdraw?openid='+lastopenid,
		type: 'get',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if (obj.status.code == 1) {
				//全部点亮,还未抽奖
				if(lastopenid==openid){
					//当头像全部点亮时,注册撕名牌事件
					//点击揭名牌进行判断
					$('.bord_cover').on('click',function(){
						//抽奖
						luck_draw();
					});
				}
			}else if (obj.status.code == 9999){
				//如果已结束,则直接跳转页面
				window.location = 'active_index.html?openid='+origopenid;
			}else if(obj.status.code == 2){
				var lockStatusStr;
				var luckdrawcode = obj.basedata.luckdrawcode;
				//全部点亮,并已抽奖
				if(lastopenid==openid){
					if(obj.basedata.name == '2'){
						lockStatusStr = '<p class="bord_message"><em>iphone6 plus</em></p>';
					}else{
						lockStatusStr = '<p class="bord_message"><em>'+luckdrawcode+'元</em>话费</p>';
						$('.edu').attr('src','../img/'+luckdrawcode+'yuan.png');
						$('.give_hint.clear').text('话费将在15分钟之内充值到您的手机上，请您正确填写手机号码，并注意查收话费。');
					}
					luckdrawid = obj.basedata.luckdrawid;
					$('#lock_status').html(lockStatusStr);
					//头像全部点亮揭名牌
					$('.left_hand').animate({
							top: 'toggle'},
							100);
					$('.bord_cover').slideUp('900');

					//注册奖品点击事件
					$('#lock_status').on('click',function(){
						$('body').scrollTop(0);
						$('.get_winner').fadeIn('fast');
					});
				}else{
					if(obj.basedata.name == '2'){
						lockStatusStr = '<p class="others_page">Ta已中大奖</p><h3><em>iPhone6 Plus</em></h3><p>快来参与，100%中奖哦！</p>';
					}else{
						lockStatusStr = '<h3 class="winner_other">Ta已中奖<em>10元</em>话费</h3><p>快来参与，100%中奖哦！</p>';
					}
					$('#lock_status').html(lockStatusStr);
					//掀开画布
					$('.left_hand').animate({
							top: 'toggle'},
							100);
					$('.bord_cover').slideUp('900');
				}
				
			}else if(obj.status.code == 3){
					var lockStatusStr;
					var luckdrawcode = obj.basedata.luckdrawcode;
					//全部点亮,并已抽奖
					if(lastopenid==openid){
						lockStatusStr = '<h3 class="winner_geted">您已领奖</h3><p>立即分享好友参与，100%中奖哦！</p>';
					}else{
						if(obj.basedata.name == '2'){
							lockStatusStr = '<p class="others_page">Ta已中大奖</p><h3><em>iPhone6 Plus</em></h3><p>快来参与，100%中奖哦！</p>';
						}else{
							lockStatusStr = '<h3 class="winner_other">Ta已中奖<em>'+luckdrawcode+'元</em>话费</h3><p>快来参与，100%中奖哦！</p>';
						}
					}
					$('#lock_status').html(lockStatusStr);
					//掀开画布
					$('.left_hand').animate({
							top: 'toggle'},
							100);
					$('.bord_cover').slideUp('900');

			}else{
				alert('服务器错误');
			}
			
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}


//抽奖
function luck_draw(){
	$.ajax({
		url: API_URL+'luckdraw/pleaseluckdraw?openid='+lastopenid,
		type: 'get',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			if(obj.status.code == 0 || obj.status.code == 1) {
				var luckdrawcode = obj.basedata.luckdrawcode;
				//alert('中奖:'+luckdrawcode);
				luckdrawid = obj.basedata.luckdrawid;
				if(obj.basedata.name == '2'){
					lockStatusStr = '<p class="bord_message"><em>iphone6 plus</em></p>';
				}else{
					lockStatusStr = '<p class="bord_message"><em>'+luckdrawcode+'元</em>话费</p>';
					$('.edu').attr('src','../img/'+luckdrawcode+'yuan.png');
					$('.give_hint.clear').text('话费将在15分钟之内充值到您的手机上，请您正确填写手机号码，并注意查收话费。');
				}
				$('#lock_status').html(lockStatusStr);
				//头像全部点亮揭名牌
				$('.left_hand').animate({
						top: 'toggle'},
						100);
				$('.bord_cover').slideUp('900');
				//领奖蒙层 撕名牌之后2S自动显示||撕名牌当时未领奖 之后点击面板弹出此蒙层
				setTimeout(function(){
					$('body').scrollTop(0);
					$('.get_winner').fadeIn('fast');
				},2000);

				//注册奖品点击事件
				$('#lock_status').on('click',function(){
					$('body').scrollTop(0);
					$('.get_winner').fadeIn('fast');
				});

			}else if(obj.status.code == -2) {
				var lockStatusStr = '<h3 class="winner_geted">已抢完</h3><p>奖品已被抢完，期待您下次参与</p>';
				$('#lock_status').html(lockStatusStr);
				//掀开画布
				$('.left_hand').animate({
						top: 'toggle'},
						100);
				$('.bord_cover').slideUp('900');
			}else if (obj.status.code == 9999){
				//如果已结束,则直接跳转页面
				window.location = 'active_index.html?openid='+origopenid;
			}else{
				alert('服务器错误');
			}
		},
		error: function (data){
			console.log("服务器异常!");
		}
	});
}

//领奖
function luck_succ(){
	$.ajax({
		url: API_URL+'award/pleaseaward?luckdrawid='+luckdrawid+'&openid='+lastopenid+'&phone='+$('#telphonenumber').val()+'&vercode='+$('#validate').val()+'&origopenid='+origopenid,
		type: 'get',
		dataType: "JSON",
		data:'',
		async: false,
		success: function (obj){
			//alert(obj.status.code);
			if(obj.status.code == 0 || obj.status.code == 1) {
				$('.person_inforcontent').after('<img class="edu" id="suc_img" src="../img/success_push.png" alt=""><p class="success_mess" id="suc_mess">话费将在15分钟内到账，敬请查收。把福利传递给好友们吧！</p>');
				$('.get_winner').hide('fast');
				$('body').scrollTop(0);
				$('.congratulate').fadeIn('fast');
			}else if(obj.status.code == -3){
				alert('验证码不正确');
			}else if(obj.status.code == -1){
				alert('奖券不存在！');
			}else if (obj.status.code == 9999){
				//如果已结束,则直接跳转页面
				window.location = 'active_index.html?openid='+origopenid;
			}else{
				alert('服务器错误,请稍后再试!');
			}
		},
		error: function (data){
			alert('服务器错误,请稍后再试!');
		}
	});
}

//页面JS效果
function show_load(){
	//获取设备高度在弹蒙层的时候设置body高度
	var CHeight = $(window).height();
	//活动规则按钮滑入
	$('.active_rules_content').animate({left: "0"},1000,function(){
		setInterval(function(){ 
			$('.active_rules_content div').fadeOut(200).fadeIn(200); },400);
		}).on('click',function(){
		window.location = 'active_rules.html';
	  });
	//超超奔跑状态
	$('#running_man').animate({
		width: '+=50',
		height: '+=50',
		opacity: 1},
		800,function(){
			//500毫秒后调用超超颤抖方法
			setTimeout(function(){demoStart();},500);		
	});
	//右手颤抖
	function leftRight(o, r) {
	 o.animate({ right: (r < 0 ? '-' : '+') + "=8px" }, '3000', 'linear', function () { leftRight($(this), -1 * r) });
	}
	$('.right_hand').each(function () {
		 var o = $(this);
		 var r = Math.random();
		 leftRight(o, r > 0.5 ? -1 : 1);
	 });
	//左手颤抖
	function upDown(o, r) {
	 o.animate({ top: (r < 0 ? '-' : '+') + "=8px" }, '3000', 'linear', function () { upDown($(this), -1 * r) });
	}
	$('.left_hand').each(function () {
		 var o = $(this);
		 var r = Math.random();
		 upDown(o, r > 0.5 ? -1 : 1);
	 });
	//按钮颤抖
	$('#running_man').jrumble();
	var demoStart = function(){
	$('#running_man').trigger('startRumble');
	setTimeout(demoStop, 1800);
	};
	
	var demoStop = function(){
		$('#running_man').trigger('stopRumble');
		setTimeout(demoStart, 1800);
	};
	//获奖名单滚动
	$(".scroll_content").myScroll({
		speed:40, //数值越大，速度越慢
		rowHeight:31 //li的高度
	});
	//当填写手机号及验证码时隐藏图片
	$('#telphonenumber').focus(function(){
		$('.edu').slideUp('fast');
	});
	$('#telphonenumber').blur(function(){
		$('.edu').slideDown('fast');
		var val = $(this).val();
		validatePhone(val);
	});
	$('#validate').focus(function(){
		$('.edu').slideUp('fast');
	});
	
	$('.left_hand').on('click',function(){
		$('.bord_cover').click();
	})
	//关闭
	$('.cancle').on('click',function(){
		$('.get_winner').hide();
		$('.congratulate').hide();	
	});
	//当已经点亮一个头像后继续点亮时显示此提示
	$('.person_content li').on('click',function(){
		$('body').scrollTop(0);
		$('.share_mask').fadeIn('fast');
	})
	
	$('.suggest_mask').on('click',function(){
		$('.suggest_mask').fadeOut('fast');
	});
	//立即分享显示
	$('#share_btn').on('click',function(){
		$('body').scrollTop(0);
		$('.share_mask').fadeIn('fast');
	});
	//邀请好友
	$('#call_friend_btn').on('click',function(){
		$('body').scrollTop(0);
		$('.share_mask').fadeIn('fast');
	});
	//我也要参加
	$('#play_into').on('click',function(){
		//$('body').scrollTop(0);
		//$('.share_mask').fadeIn('fast');
		window.location = 'share_active.html?origopenid='+origopenid+'&lastopenid='+openid;
	});
	$('.share_mask').on('click',function(){
		$('.share_mask').fadeOut('fast');
	});
}


//分享成功,记录
function shareSuccess(){
	var share_success_url = API_URL+'forward/pleaseforward?origopenid='+origopenid+'&lastopenid=';
	if(openid!=null && openid!=''){
		share_success_url = share_success_url + openid + '&type=2';
	}else{
		share_success_url = share_success_url + lastopenid + '&type=2'
	}

	$.ajax({
		url: share_success_url,
		type: 'get',
		dataType: "JSON",
		data:'',
		async: false
	});
}
//随机生成手机号
function randomPhone(){
	var sumStr = '';
	for (var k = 0; k < 101; k++) {
		var _realnum = [30,31,33,34,35,36,37,38,39,45,47,50,51,52,53,55,56,57,58,59,82,83,85,86,87,88,89];
		var _winnum = ['10元','5元','2元'];
		var _result = '1';
		var winner_text = '10元';
		for(var i=0;i<100; i++){
			var _num = parseInt(Math.random()*100);
			if ( _num < 27) {
				_result += _realnum[_num];
				break;
			}
		}
		_result +='****';

		for(var j=0;j<100; j++){
			var mid_num = parseInt(Math.random()*10000);
			if ( mid_num > 1000) {
				_result += mid_num;
				break;
			}
		}
		for(var y=0;y<100; y++){
			var win_num = parseInt(Math.random()*10);
			if ( win_num < 3) {
				winner_text = _winnum[win_num];
				break;
			}
		}
		
		sumStr += '<li><span>'+_result+'</span><em><i>'+winner_text+'</i>话费</em></li>';
	};	
	$('ul.winners_list').html(sumStr);
	$('ul.winners_list li:nth-child(12)').html('<span>159****4831</span><em><i>5元话费</i></em>');
	$('ul.winners_list li:nth-child(70)').html('<span>188****0487</span><em><i>10元话费</em>');
}
//验证手机号码
function validatePhone(phone) {
　　if (phone==""){
　　alert("电话号码不能为空!");
　　return false;
　　}
　　var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
 if (reg.test(phone)) {
		console.log("手机号码正确！")
 }else{
      alert("手机号码有误~");
 };
}

$("#get_btn_").click(function(){
	luck_succ();
})